///////////////////////////////////////////////////////////////////////////
// Copyright 2010 Arabic Game Developer Network, All rights reserved
// www.agd-online.com - info@agdn-online.com
//
// File: gamestates.cpp
// Description:
//
///////////////////////////////////////////////////////////////////////////

// This file is part of kvalleyX.
//
// kvalleyX is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// kvalleyX is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with kvalleyX.  If not, see <http://www.gnu.org/licenses/>.

#include "gamestates.h"
#include "game.h"

/////////////Game states implementation ////////////////////////////////////////
void RedGameState::update()
{
	///TODO: fire an event 1 if the action button is pressed
	CoreLib::StateEvent event(CoreLib::StateEvent::STATE_TRANSIT, "event1");
	CoreLib::Core2& core = CoreLib::Core2::getInstance();
	core.getEventManager().fireEvent(event);
}

void RedGameState::render()
{
	///TODO: paint the screen red
}

void BlueGameState::update()
{
}

void BlueGameState::render()
{
}

void GreenGameState::update()
{
}

void GreenGameState::render()
{
}

/////////////GameStateFlow implementation //////////////////////////////////////

CoreLib::GameState* GameStateFlow::createState(
	const CoreLib::GameStateDescriptor& descriptor) const
{
	CoreLib::GameState* state = NULL;
	std::string stateName = descriptor.getStateName();

	///TODO: add the descriptors to a list in Game::initState, and use the
	/// GameStateDescriptor::operator== instead for comparison.
	if(stateName == StateFlow::START_STATE)
		state = new RedGameState();
	
	else if(stateName == "red")
		state = new RedGameState();
	else if(stateName == "blue")
		state = new BlueGameState();
	else if(stateName == "green")
		state = new GreenGameState();

	///NOTE: objects are deleted by the StateFlow class.
	return state;
}

void GameStateFlow::initStates()
{
	using namespace CoreLib;
	// state descriptors are used to describe states, and are
	// passed to the stateFlow object. The StateFlow::CreateState
	// must be implemented in order to get the descriptor and
	// return a State object.
	
	GameStateDescriptor* red = new GameStateDescriptor("red");
	GameStateDescriptor* green = new GameStateDescriptor("green");
	GameStateDescriptor* blue = new  GameStateDescriptor("blue");
	
	//events
	StateEvent event1(StateEvent::STATE_TRANSIT, "event1");
	StateEvent event2(StateEvent::STATE_TRANSIT, "event2");
	StateEvent event3(StateEvent::STATE_TRANSIT, "event3");
	
	addTransition(red, green, event1);
	addTransition(green, blue, event2);
	addTransition(blue, green, event3);
}